<!-- 导入多个jira -->
<script setup lang="ts">
import { ref } from "vue"
import { CirclePlus, Upload } from "@element-plus/icons-vue"
import { ElMessage, DrawerProps } from "element-plus"
import dayjs from "dayjs"

const today = (): string => {
  return dayjs().format("YYYY-MM-DD")
}

import SseConnection from "@/utils/sseUtil"

// 导入结果
const results = ref<any>([])
const sseUtil = new SseConnection()

const messages = ref<string[]>([]) // 消息列表
const isConnected = ref(false) // 是否连接中

const day = ref(today())

// 开始 SSE 请求
const startImport = () => {
  if (!day.value.trim()) {
    ElMessage({
      message: "请选择导入日期",
      type: "warning"
    })
    return
  }

  messages.value = [] // 清空消息

  sseUtil.connect({
    url: `/jira-sse/importDay`,
    params: { day: day.value },
    onMessage: (message) => {
      messages.value.push(message)
      results.value.push({ message: message })
    },
    onOpen: () => {
      console.log("SSE 连接成功")
      isConnected.value = true
    },
    onError: () => {
      alert("连接发生错误或断开")
      isConnected.value = false
    },
    onComplete: () => {
      console.log("SSE 任务完成")
      isConnected.value = false
    }
  })
}

// 定义触发父组件的事件
const emit = defineEmits<{
  (event: "set-loading", value: boolean): void // 设置父组件的 loading 状态
  (event: "operation-complete"): void // 操作完成的事件
}>()

const drawer = ref(false)
const direction = ref<DrawerProps["direction"]>("rtl")

const openImportJira = () => {
  drawer.value = true
  results.value = []
  day.value = today()
}
const importJira = () => {
  results.value = []
  startImport()
}

const confirmClick = () => {
  drawer.value = false
  emit("operation-complete")
}
// endregion
</script>

<template>
  <el-divider direction="vertical" />
  <el-button size="small" type="primary" :icon="CirclePlus" @click="openImportJira">按天导入</el-button>

  <!-- 按天导入 jira 抽屉 start :with-header="false"-->
  <el-drawer
    v-model="drawer"
    :direction="direction"
    size="60%"
    :close-on-click-modal="false"
    :show-close="false"
    :with-header="false"
  >
    <template #default>
      <div>
        <el-date-picker v-model="day" type="date" placeholder="选择日期" value-format="YYYY-MM-DD" />
        <el-button class="mx-2" type="primary" :icon="Upload" @click="importJira" :disabled="isConnected"
          >导入当天</el-button
        >
      </div>
      <div />
      <div class="table-wrapper">
        <el-table :data="results" stripe :border="true">
          <el-table-column prop="message" label="导入结果  →   (基于SSE推送)" align="left">
            <template #default="scope">
              <div v-html="scope.row.message" />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button type="primary" @click="confirmClick" :disabled="isConnected">关闭</el-button>
      </div>
    </template>
  </el-drawer>
  <!--按天导入 jira 抽屉 end-->
</template>

<style scoped lang="scss">
.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
}

.table-wrapper {
  padding: 10px;
}
</style>
